<template>
  <div class="mnt-dialog form-container">
    <el-form ref="form" :model="formModel" label-width="120px" :rules="rules">
      <el-row style="text-align: center">
        <el-col :span="12">
          <el-form-item label="车辆照片代码:" prop="pictureNo">
            <el-input v-model="formModel.pictureNo" :disabled="disabledNo"/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-upload
      style="display:inline-block"
      ref="upload"
      action=""
      :file-list="fileList"
      :on-change="handleChange"
      :on-remove="handleRemove"
      :http-request="uploadSectionFile"
      :auto-upload="false">
      <el-button slot="trigger" type="primary">选取照片</el-button>
      <el-button type="success" @click="submitUpload">上传</el-button>
    </el-upload>
    <div class="imageDiv" v-if="showImage">
      <h3>原照片预览:</h3>
      <img class="image" :src="imageSrc" style="width:50%;height:50%">
    </div>
  </div>
</template>

<script>
  export default {
    name: 'VehiclePhotosInfoMnt',
    props: ['pageType', 'editData'],
    data() {
      return {
        imageSrc:'',
        showImage :false,
        disabledNo :false,
        fileList: [],
        formModel: {
          tmBasPhotosId: '',
          pictureNo: '',
        },
        rules: {

        }
      }
    },
    computed: {

    },
    mounted() {
      this.$form = this.$refs.form
      this.init()
      this.imageSrc = 'data:image/jpeg;base64,'+this.editData.picture
    },
    methods: {
      init() {
        this.$form = this.$refs.form
        const { pageType, editData, formModel } = this
        if (pageType === 'edit') {
          this.showImage = true
          this.assignData(editData, formModel)
          this.disabledNo = true
        }
        this.initRules()
      },
      closeDialog() {
        this.$parent.$emit('update:visible', false)
      },
      initRules() {
        const REQUIRED_MSG = this.CONST.REQUIRED_MSG
        this.rules = {
          pictureNo: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
        }
        this.$nextTick(_ => {
          this.$form.clearValidate()
        })
      },
      handleChange(file, fileList) {
        this.fileList = fileList
      },
      handleRemove(file, fileList) {
        this.fileList = fileList
      },
      submitUpload() {
        this.$refs.form.validate(valid => {
          if (valid) {
            if(this.fileList.length==0){
              this.$message.warning('请选取一张照片!')
              return false
            }
            if(this.fileList.length>1){
              this.$message.warning('单次只能上传一张照片!')
              return false
            }
            this.$refs.upload.submit();
          }
        })
      },
      uploadSectionFile(param) {
        var fileObj = param.file;
        const isImage = (fileObj.type == 'image/jpeg') || (fileObj.type == 'image/jpg') || (fileObj.type == 'image/gif') || (fileObj.type == 'image/png');
        if (!isImage) {
          this.$message.warning('上传的图片只能是 JPEG/JPG/GIF/PNG 格式!');
          return false;
        }
        const operateType = this.pageType === 'edit' ? '02' : '01'
        const url = '/tmBasPhotos/maintainTmBasPhotos'
        //FormData 对象
        var form = new FormData();
        form.append("file", fileObj);
        form.append("tmBasPhotosId", this.formModel.tmBasPhotosId);
        form.append("pictureNo", this.formModel.pictureNo);
        form.append("operateType", operateType);
        const tip = '图片上传成功!'
        this.post(url, form).then(res => {
          this.$info(tip, () => {
            this.$emit('mntSuc')
            this.closeDialog()
          })
        })
      },
    }
  }
</script>

<style lang="scss" scoped>
  .el-button--medium {
    width: 100px;
  }
  .imageDiv{
    padding-top:20px;
  }
  .image{
    padding-top:10px;
  }
</style>
